using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using System.Xml;
using Caliburn.Micro;
using ChessKit.ChessLogics;
using System.Linq;
using ChessKit.Common;

namespace EmraldClient
{
  public class Problem : PropertyChangedBase
  {
    private readonly List<Move> _moves;
    public string Position { get; private set; }
    public ReadOnlyCollection<Move> Moves { get; private set; }

    public Problem()
    {
      _moves = new List<Move>();
      Moves = new ReadOnlyCollection<Move>(_moves);
    }
    public void AddPosition(string fen)
    {
      if (Position == null)
        Position = fen;
    }
    public void AddMove(Move move)
    {
      _moves.Add(move);
    }

    public Problem(string fen, params string[] moves)
    {
      Position = fen;
      Moves = moves.Select(m =>
        {
          var s = m.Split('-');
          return new Move(
            ChessKit.Common.Position.Parse(s[0]),
            ChessKit.Common.Position.Parse(s[1]),
            PieceType.Queen);
        }).ToList().AsReadOnly();
    }
    public bool IsReversed { get; set; }
    public string Id { get; set; }

    public TimeSpan Time { get; set; }
    public TimeSpan Ex { get; set; }
    public double Succ { get; set; }
    public double Fail { get; set; }
    public int TacRating { get; set; }
    public int PosRD { get; set; }
    public int TacRD { get; set; }
    public int PosRating { get; set; }

    #region ' IsSolved Property '

    public bool? IsSolved
    {
      get { return _isSolved; }
      set
      {
        if (_isSolved == value) return;
        _isSolved = value;
        NotifyOfPropertyChange(() => IsSolved);
      }
    }

    private bool? _isSolved;

    #endregion

    public override string ToString()
    {
      return string.Format("new Problem(\"{0}\", {1})", Position, 
        string.Join(", ", Moves.Select(m => string.Format("\"{0}\"", m))));
    }
  }
}

